﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using NPOI.HSSF.UserModel;
using System.IO;

namespace HermesWinForm
{
    public partial class DownloadShipedInfo : Form
    {
        bool SelectSite = false;
        bool SelectStore = false;
        bool SelectCarrier = false;

        public DownloadShipedInfo()
        {
            InitializeComponent();
        }

        private void DownloadShipedInfo_Load(object sender, EventArgs e)
        {
            string[] carrierNames = CarrierInfo.GetCarrierNames();
            for (int i = 0; i < carrierNames.Length; ++i)
            {
                cbCarriers.Items.Insert(i, carrierNames[i]);
            }

            string[] siteNames = SiteInfo.GetSiteName();
            for (int i = 0; i < siteNames.Length; ++i)
            {
                cbSites.Items.Insert(i, siteNames[i]);
            }

            cbStore.Enabled = false;
        }

        private void cbSites_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbSites.SelectedItem.ToString() != "")
            {
                SelectSite = true;
                cbStore.Enabled = true;

                for (int i = cbStore.Items.Count - 1; i >= 0; --i)
                {
                    cbStore.Items.RemoveAt(i);
                }

                string[] storeNames = StoreInfo.GetStoreName();
                int j = 0;
                for (int i = 0; i < storeNames.Length; ++i)
                {
                    if (storeNames[i].Contains(cbSites.SelectedItem.ToString()))
                    {
                        cbStore.Items.Insert(j, storeNames[i]);
                        ++j;
                    }
                }

                if (cbStore.SelectedItem != null && cbStore.SelectedItem.ToString() != "")
                {
                    SelectStore = true;

                }
                else
                {
                    SelectStore = false;
                }

            }
            else
            {
                SelectSite = false;
                cbStore.Enabled = false;
            }

            if (SelectSite && SelectStore && SelectCarrier)
            {
                btnDownload.Enabled = true;
            }
            else
            {
                btnDownload.Enabled = false;
            }
        }

        private void cbStore_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbStore.SelectedItem.ToString() != "")
            {
                SelectStore = true;

            }
            else
            {
                SelectStore = false;
            }

            if (SelectSite && SelectStore && SelectCarrier)
            {
                btnDownload.Enabled = true;
            }
            else
            {
                btnDownload.Enabled = false;
            }
        }

        private void btnDownload_Click(object sender, EventArgs e)
        {
            int siteID = SiteInfo.GetSiteID(cbSites.SelectedItem.ToString());
            int storeID = StoreInfo.GetStoreID(cbStore.SelectedItem.ToString());
            int carrierID = CarrierInfo.GetCarrierID(cbCarriers.SelectedItem.ToString());
            DateTime startDate = dtStart.Value;
            DateTime endDate = dtEnd.Value.AddDays(1);
            HermesWinForm.OrdersServiceReference.FulfillmentInfo[] infos = null;

            try
            {
                OrdersServiceReference.SalesOrderSoapClient orderClient = new OrdersServiceReference.SalesOrderSoapClient();
                infos = orderClient.GetFulfilInfos(siteID, storeID, carrierID, startDate, endDate);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            if (infos == null)
            {
                MessageBox.Show("Data is Empty");
            }
            else
            {

                DialogResult result = saveOrdersFile.ShowDialog();
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    ExportEUBFulfilmentInfo(saveOrdersFile.FileName, infos, cbCarriers.SelectedItem.ToString());
                }
                else
                {
                    MessageBox.Show("选择的保存文件失败。");
                }
            }
        }

        private void ExportEUBFulfilmentInfo(string fileName, HermesWinForm.OrdersServiceReference.FulfillmentInfo[] infos, string carrierName)
        {
            //创建xls文件
            HSSFWorkbook workbook = new HSSFWorkbook();
            //创建sheet表单，默认名称为sheet1
            HSSFSheet sheet = workbook.CreateSheet(carrierName +"Fulfilment Data");


            //创建行，并设定第一行的表头
            HSSFRow rowTitle = sheet.CreateRow(0);

            rowTitle.CreateCell(0, HSSFCellType.STRING).SetCellValue("order-id");
            rowTitle.CreateCell(1, HSSFCellType.STRING).SetCellValue("order-item-id");
            rowTitle.CreateCell(2, HSSFCellType.STRING).SetCellValue("ship-date");
            rowTitle.CreateCell(3, HSSFCellType.STRING).SetCellValue("carrier-code");
            rowTitle.CreateCell(4, HSSFCellType.STRING).SetCellValue("carrier-name");
            rowTitle.CreateCell(5, HSSFCellType.STRING).SetCellValue("tracking-number");
            rowTitle.CreateCell(6, HSSFCellType.STRING).SetCellValue("ship-method");
            int rowNum = 0;
            foreach (HermesWinForm.OrdersServiceReference.FulfillmentInfo temp in infos)
            {
                HSSFRow rowContent = sheet.CreateRow(rowNum + 1);//创建内容行

                //对第1行对应的单元格分别设定单元格内容属性和填充对应的值
                rowContent.CreateCell(0, HSSFCellType.STRING).SetCellValue(temp.Ordernumber);
                rowContent.CreateCell(1, HSSFCellType.STRING).SetCellValue("");
                rowContent.CreateCell(2, HSSFCellType.STRING).SetCellValue(temp.ShippingDate.ToString("yyyy-MM-dd"));
                rowContent.CreateCell(3, HSSFCellType.STRING).SetCellValue("");
                rowContent.CreateCell(4, HSSFCellType.STRING).SetCellValue(carrierName);
                rowContent.CreateCell(5, HSSFCellType.STRING).SetCellValue(temp.TrackingNo);
                rowContent.CreateCell(6, HSSFCellType.STRING).SetCellValue("");
                rowNum++;//动态增加行号
            }

            //写入excel文件
            using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
            {
                workbook.Write(fs);//把流写入文件
            }

            MessageBox.Show(rowNum  + "Item to Export");//实际导出的数据比定义的行号少1            
        }

        private void cbCarriers_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbCarriers.SelectedItem != null && cbCarriers.SelectedItem.ToString() != "")
            {
                SelectCarrier = true;
            }
            else
            {
                SelectCarrier = false;
            }

            if (SelectSite && SelectStore && SelectCarrier)
            {
                btnDownload.Enabled = true;
            }
            else
            {
                btnDownload.Enabled = false;
            }
        }


    }
}
